<?php

namespace app\controller\admin\sky;

use app\model\api\SkyDiy;
use app\model\api\SkyDiyGoods;
use laytp\controller\Backend;
/**
 * Class Diy
 * @package app\controller\admin\sky
 */
class Diygoods extends Backend
{
    /**
     * api_log模型对象
     * @var \app\model\api\Log
     */
    protected $model;
    protected $hasSoftDel=0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法


    public function _initialize()
    {
        $this->model = new SkyDiyGoods();
    }


    /**
     * 列表
     * @return false|mixed|string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $where   = $this->buildSearchParams();
        $order   = $this->buildOrder();
        $allData = $this->request->param('all_data');
        if ($allData) {
            $data = $this->model->where($where)->order($order)->cache(true)->select()->toArray();
        } else {
            $diyModelName = (new SkyDiy())->cache(true)->column('model_name','id');
            $limit = $this->request->param('limit', 10);

            $diyModelIds = array_keys($diyModelName);
            $where[] = ['model_id','in',$diyModelIds];
            $data  = $this->model->where($where)->order($order)->paginate($limit)->toArray();
            array_walk($data['data'],function(&$item,$key) use ($diyModelName){
               $item['model_name'] = $diyModelName[$item['model_id']];
            });
        }
        return $this->success('数据获取成功', $data);
    }

}